package com.example.jm24;

import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class Setting extends AppCompatActivity {
    private Button btn;
    private EditText txt1,txt2,txt3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Bundle b=getIntent().getExtras();
        //获取Bundle的信息
        final String info2=b.getString("username");
        //final String url=b.getString("url");
        findviews();
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String old =txt1.getText().toString().trim();//.trim()去掉字符串两端的bai多du余的空格,注册使用，登录就不必再验证
                String pass=txt2.getText().toString().trim();
                String newpass = txt3.getText().toString().trim();
                //判断输入框内容
                if(TextUtils.isEmpty(old)){
                    Toast.makeText(getApplication(), "请输入旧密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(pass)){
                    Toast.makeText(getApplication(), "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(newpass)){
                    Toast.makeText(getApplication(), "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if(!pass.equals(newpass)){
                    Toast.makeText(getApplication(), "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                    return;}
                new Thread(new Runnable() {
                    private static final String TAG = "测试";

                    @Override
                    public void run() {
                        String old =txt1.getText().toString().trim();//.trim()去掉字符串两端的bai多du余的空格,注册使用，登录就不必再验证
                        String pass=txt2.getText().toString().trim();
                        UserDao ud = new UserDao();
                        //boolean result =ud.register(n,psw);
                            //UserDao u = new UserDao();
                            //boolean t=u.register(n, psw);
                            if (ud.Updatepass(getIntent().getStringExtra("username"),old,pass)) {
                                Looper.prepare();
                                Toast.makeText(getApplication(), "插入数据表成功", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }else{
                                Looper.prepare();
                                Toast.makeText(getApplication(), "修改失败", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        /*if (!result){
                            Looper.prepare();
                            Toast toast = Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT);
                            toast.show();
                            Looper.loop();
                        }*/
                            //以上为jdbc注册
                    }
                }).start();
            }
        });
    }
    private void findviews() {
        txt1=findViewById(R.id.old_password);
        txt2=findViewById(R.id.new_password);
        txt3=findViewById(R.id.new_password2);
        btn=findViewById(R.id.btn);
    }
}
